package com.xudow.app.dynamicstate_old.module.user;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.jakewharton.rxbinding.view.RxView;
import com.jude.beam.bijection.RequiresPresenter;
import com.jude.beam.expansion.list.BeamListFragment;
import com.jude.beam.expansion.list.ListConfig;
import com.jude.easyrecyclerview.adapter.BaseViewHolder;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.jude.utils.JUtils;
import com.xudow.app.R;
import com.xudow.app.XApplication;
import com.xudow.app.dynamicstate_old.domain.Config;
import com.xudow.app.dynamicstate_old.domain.MessageEvent;
import com.xudow.app.dynamicstate_old.domain.entity.Dynamic;
import com.xudow.app.dynamicstate_old.module.follow.DynamicDetailActivity;
import com.xudow.app.dynamicstate_old.module.follow.NewDynamicActivity;
import com.xudow.app.dynamicstate_old.module.user.viewholder.DynamicContentViewHolder;
import com.xudow.app.dynamicstate_old.module.user.viewholder.PostLongViewHolder;
import com.xudow.app.dynamicstate_old.module.user.viewholder.PostNormalViewHolder;
import com.xudow.app.dynamicstate_old.module.user.viewholder.RepostViewHolder;
import com.xudow.app.ui.LoginActivity;

import org.greenrobot.eventbus.EventBus;

import static com.xudow.app.dynamicstate_old.module.DynamicStateFragment.EVENT_GO_TO_DISCOVER;

/**
 * Created by ske on 2016/6/10.
 */
@RequiresPresenter(UserDynamicPresenter.class)
public class UserDynamicFragment extends BeamListFragment<UserDynamicPresenter, Dynamic> implements RecyclerArrayAdapter.OnItemClickListener {
    private static final int POST_NORMAL = 1;
    private static final int POST_LONG = 2;
    private static final int REPOST = 3;

    public static UserDynamicFragment newInstance(long userId) {
        Bundle args = new Bundle();
        args.putLong("userId", userId);
        UserDynamicFragment fragment = new UserDynamicFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        userProfileId = getArguments().getLong("userId");
        return super.onCreateView(inflater, container, savedInstanceState);
    }

    long userProfileId;

    @Override
    public ListConfig getConfig() {
        return super.getConfig().setRefreshAble(false)
                .setContainerErrorView(getEmptyView())
                .setContainerEmptyView(getEmptyView());
    }

    @Override
    public BaseViewHolder getViewHolder(ViewGroup parent, int viewType) {
        DynamicContentViewHolder viewHolder = null;
        if (viewType == POST_LONG) {
            viewHolder = new PostLongViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == POST_NORMAL) {
            viewHolder = new PostNormalViewHolder(getPresenter().getAdapter(), parent);
        } else if (viewType == REPOST) {
            viewHolder = new RepostViewHolder(getPresenter().getAdapter(), parent);
        }
        return viewHolder;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        getPresenter().getAdapter().setOnItemClickListener(this);
        getListView().addItemDecoration(new DividerDecoration(getResources().getColor(R.color.gray100), JUtils.dip2px(12)));
    }

    private View getEmptyView() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.view_no_dynamic, null, false);
        if (userProfileId == XApplication.getInstance().getUserProfileId())
            RxView.clicks(view.findViewById(R.id.tag_write))
                    .subscribe(v -> {
                        startActivity(new Intent(getContext(), NewDynamicActivity.class));
                    });
        else
            view.findViewById(R.id.tag_write).setVisibility(View.GONE);
        return view;
    }

    @Override
    public int getViewType(int position) {
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        // 1：长文，2：普通
        int type = dynamic.getType();
        // 上级动态主键, 为0说明，这条动态不是转发的
        long pId = dynamic.getpId();
        if (type == 1) {
            if (pId == 0) {
                return POST_LONG;
            } else
                return REPOST;
        } else if (type == 2) {
            if (pId == 0) {
                return POST_NORMAL;
            } else {
                // Dynamic 字段中没有办法判断上级Dynamic是什么类型，所以共用一套布局，并隐藏不需要的布局
                return REPOST;
            }
        }
        return 0;
    }

    @Override
    public void onItemClick(int position) {
        Intent intent = new Intent(getActivity(), DynamicDetailActivity.class);
        Dynamic dynamic = getPresenter().getAdapter().getItem(position);
        intent.putExtra("dynamic", dynamic);
        ((Activity) getContext()).startActivityForResult(intent, Config.Code_NeedRefresh);
    }

    @Override
    public void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == Activity.RESULT_OK) {
            getPresenter().onRefresh();
        }
    }
}
